/**
 *@Description 物流公司配置
 *@Author chenxq
 *@Date 201516-01-08
 */
var companyCtrl  = new XFApp.client();
XFApp.client.prototype.company = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryByPageURL : basePath+"/logistics/company/queryPage",
        getCompanyURL : basePath+"/logistics/company/detail",
        saveCompanyURL : basePath+"/logistics/company/save",
        delCompanyURL : basePath+"/logistics/company/delete"
    },
    queryPage : function(){
        _this = this;
        if(!!_this.page){
            _this.page.page("remote",0,$('#searchForm').serialize());
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                currentPageIndex : Number($("#currentPageIndex").val())-1,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: $('#searchForm').serialize(),
                    success: function (data, pageIndex) {
                        $("#currentPageIndex").val(data.result.pageNo);
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(null == data
                                || null == data.result
                                || null == data.result.itemList
                                || data.result.itemList.length < 1){
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }else{
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }
                        }else{
                            webDialogUtil.simpleAlert("数据查询失败!");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询失败!");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    formValidate : function(){
        $("#editCompanyForm").validate({
            rules: {
                logisticsName: "required",
                concatName: "required",
                concatPhone: {
                    required:true
                },
                concatEmail: {
                    required: true,
                    email:true
                },
                address: "required"
            },
            messages: {
                logisticsName: "请填写公司名称",
                concatName: "请填写联系人姓名",
                concatPhone:  {
                    required : "请填写联系电话"
                },
                concatEmail: {
                    required : "请填写联系邮箱",
                    email : "邮箱格式错误"
                },
                address: "请填写公司地址"
            }
        });
        $("#editCompanyForm").validate();
    },
    editCompanyView : function(id,typeId){
        _this = this;
        if(null == id){
            return this.editCompanyDialog(null,typeId);
        }
        $.ajax({
            url:_this.urlItems.getCompanyURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(false == data.isSuccess){
                    window.webDialogUtil.alert("提示","获取数据失败");
                    return;
                }
                _this.editCompanyDialog(data.result,typeId);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    editCompanyDialog : function(data,typeId){
        if(null != data){
            $("#editCompanyForm #id").val(data.id);
            $("#editCompanyForm #logisticsName").val(data.logisticsName);
            $("#editCompanyForm #concatName").val(data.concatName);
            $("#editCompanyForm #concatPhone").val(data.concatPhone);
            $("#editCompanyForm #concatEmail").val(data.concatEmail);
            $("#editCompanyForm #address").val(data.address);
            $("#editCompanyForm #updateTime").text(data.updateTime);
            $("#editCompanyForm #updatePerson").text(data.updatePerson);
        }else{
            $("#editCompanyForm #id").val("");
            $("#editCompanyForm #logisticsName").val("");
            $("#editCompanyForm #concatName").val("");
            $("#editCompanyForm #concatPhone").val("");
            $("#editCompanyForm #concatEmail").val("");
            $("#editCompanyForm #address").val("");
            $("#editCompanyForm #updateTime").text("");
            $("#editCompanyForm #updatePerson").text("");
        }
        webAlert({
            title:companyCtrl.company.titleDisplay(typeId),
            content:document.getElementById("editView"),
            button:[
                {
                    name:"保存",
                    callback:function(){
                        if($("#editCompanyForm").validate().form()){
                            companyCtrl.company.saveCompanyForm();
                        }else{
                            return false;
                        }
                    }},
                {
                    name:"关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    },
    saveCompanyForm : function(){
        _this = this;
        $.ajax( {
            url:_this.urlItems.saveCompanyURL,
            data:$('#editCompanyForm').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                window.webDialogUtil.alert("提示",data.msg);
                companyCtrl.company.queryPage();
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    titleDisplay : function(typeId){
        switch(typeId){
            case 0:return "查看";
            case 1:return "修改";
            case 2:return "新增";
            default : return "";
        }
    },
    delCompanyDialog : function(id){
        _this = this;
        webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){companyCtrl.company.delCompany(id);});
    },
    delCompany : function(id){
        _this = this;
        $.ajax({
            url:_this.urlItems.delCompanyURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(true == data.isSuccess){
                    webDialogUtil.simpleAlert(data.msg);
                    companyCtrl.company.queryPage();
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    event : function(){
        _this = this;
        $("#searchButton").on("click",function(){
            companyCtrl.company.queryPage();
        });
        companyCtrl.company.queryPage();
        this.formValidate();
    }
};
$.views.converters({

});
$(function(){
    companyCtrl.company.event();
});